package com.tj.test.sys.controller;

import com.tj.test.sys.po.Classc;
import com.tj.test.sys.service.impl.ClassService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/Class")
@Api(tags = "Class")
public class ClassController {
    @Autowired
    private ClassService service;

    @ApiOperation("查询所有班级信息")
    @RequestMapping(value = "/selectAllClass",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectAllClass(){
        Map<String,List<Classc>> map=new HashMap<>();
        List<Classc> list=service.selectAllClass();
        if (!list.isEmpty()){
            map.put("AllClass",list);
            return Result.success("查询成功",map);
        }
        return Result.error("查询失败",false);
    }

    @ApiOperation("插入班级信息")
    @RequestMapping(value = "/insertClass",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage insertClass(Classc classc){
        int temp=0;
        if (classc!=null){
            if (classc.getClassname()!=null){
                temp=service.insertClass(classc);
                if (temp>0) {return Result.success("插入班级信息成功",temp);}
                else  {return Result.error("该班级已存在",temp);}
            }
        }
        return Result.error("插入班级信息失败",temp);
    }

    @ApiOperation("删除班级信息")
    @RequestMapping(value = "/deleteClass",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deleteClass(Classc classc){
        int temp=0;
        if (classc!=null){
            if (classc.getClassname()!=null&&classc.getClassid()!=0){
                temp=service.deleteClass(classc);
                if (temp>0) {return Result.success("删除班级信息成功",temp);}
                else {return Result.error("无该班级",temp);}
            }else{
                return Result.error("缺少班级名称或班级id");
            }
        }
        return Result.error("删除班级信息失败",temp);
    }

    @ApiOperation("修改班级信息")
    @RequestMapping(value = "/updateClass",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage updateClass(Classc classc){
        int temp=0;
        if (classc!=null){
            if (classc.getClassname()!=null){
                temp=service.updateClass(classc);
                if (temp>0) {return Result.success("更新班级信息成功",temp);}
                else {return Result.error("无该班级信息",temp);}
            }
        }
        return Result.error("更新班级信息失败",temp);
    }

}
